/*
 * Copyright 2002-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.k8s.hello.service.common.http;

import com.example.k8s.hello.common.bean.RestResponse;
import com.example.k8s.hello.common.exception.ApiExEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
import org.springframework.web.service.invoker.HttpRequestValues;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 代理restclient返回结果，实现对RestResponse结果的拆箱
 */
@Slf4j
public class RestClientAdapterProxy implements HttpExchangeAdapter {

	private final RestClientAdapter restClientAdapter;


	public RestClientAdapterProxy(RestClientAdapter restClientAdapter) {
		this.restClientAdapter = restClientAdapter;
	}


	@Override
	public boolean supportsRequestAttributes() {
		return restClientAdapter.supportsRequestAttributes();
	}

	@Override
	public void exchange(HttpRequestValues requestValues) {
		restClientAdapter.exchange(requestValues);
	}

	@Override
	public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
		return restClientAdapter.exchangeForHeaders(values);
	}

	@Override
	@Nullable
	public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
		ResponseEntity<T> rspEntity = exchangeForEntity(values, bodyType);

		return rspEntity.getBody();
	}

	@Override
	public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
		ParameterizedTypeReference<RestResponse> type = new ParameterizedTypeReference<>() {};
		ResponseEntity<RestResponse> rspEntity = restClientAdapter.exchangeForEntity(values, type);

		return ResponseEntity.status(rspEntity.getStatusCode())
				.headers(rspEntity.getHeaders())
				.build();
	}

	@Override
	public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
		if (bodyType.getType().getTypeName().equals(RestResponse.class.getName())) {
			return restClientAdapter.exchangeForEntity(values, bodyType);
		}

		Type type = new ParameterizedType() {
			@Override
			public Type[] getActualTypeArguments() {
				return new Type[] { bodyType.getType() };
			}
			@Override
			public Type getRawType() {
				return RestResponse.class;
			}
			@Override
			public Type getOwnerType() {
				return null;
			}
		};

		ResponseEntity<RestResponse<T>> rspEntity = restClientAdapter.exchangeForEntity(values, ParameterizedTypeReference.forType(type));

		checkRestResponse(rspEntity.getBody());

		return ResponseEntity.status(rspEntity.getStatusCode())
				.headers(rspEntity.getHeaders())
				.body(rspEntity.getBody().data);
	}

	private static void checkRestResponse(RestResponse restResponse) {
		if (!restResponse.success()) {
			ApiExEnum.C0003.thrown(restResponse.toJson());
		}
	}

}
